package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Users;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.TransactionUtil;
import com.itheima.utils.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    @Override
    public Users findAllPermission(String userId) {
        SqlSession session = MapperFactory.getSqlSession();

        UserMapper mapper = session.getMapper(UserMapper.class);

        Users user = mapper.findAllPermission(userId);

        TransactionUtil.commit(session);

        TransactionUtil.close(session);

        return user;
    }



    @Override
    public Users login(String username, String password) {
        SqlSession session = null;
        Users user=null;

        session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);

//        password = MD5Util.md5(password);

        user = mapper.findByUsernameAndPassword(username, password);

        session.commit();

        MapperUtil.close(session);

        return user;
    }
/*
    @Override
    public List<Users> findAll(String pageNum, String pageSize) {
        SqlSession session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);

        int pageN = 1;
        int pageS = 10;
        if (StringUtils.isNotBlank(pageNum)){
            pageN = Integer.parseInt(pageNum);
        }
        if (StringUtils.isNotBlank(pageSize)){
            pageS = Integer.parseInt(pageSize);
        }
        PageHelper.startPage(pageN,pageS);

        List<Users> list = mapper.findAll();

        MapperUtil.close(session);

        return list;
    }*/

    @Override
    public void changePassword(Users users) {
        SqlSession session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);


        mapper.changePassword(users);


        TransactionUtil.commit(session);

        TransactionUtil.close(session);
    }

    //张夫影代码

    @Override
    public List<Users> findAll(String pageNum, String pageSize) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            int pageN = 1;
            int pageS = 10;
            if (pageNum != null) {
                pageN = Integer.parseInt(pageNum);
            }
            if (pageSize != null) {
                pageS = Integer.parseInt(pageSize);
            }
            PageHelper.startPage(pageN, pageS);
            //3.调用Dao层操作
            return mapper.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void save(Users userInfo) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            userInfo.setId(id);
            //3.调用Dao层操作
            mapper.save(userInfo);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            String[] ids = id.split(",");
            mapper.deleteByIds(ids);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Users findById1(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            return mapper.findByUserId(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Users> findLikeName(String name ,String pageNum ,String pageSize) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            int pageN = 1;
            int pageS = 10;
            if (pageNum != null) {
                pageN = Integer.parseInt(pageNum);
            }
            if (pageSize != null) {
                pageS = Integer.parseInt(pageSize);
            }
            PageHelper.startPage(pageN, pageS);
            //3.调用Dao层操作
            return mapper.findLikeName(name);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Map map) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            mapper.update(map);
            TransactionUtil.commit(sqlSession );
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Users findById(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            return mapper.findByRoleId(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public void update1(Users userInfo) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            mapper.update1(userInfo);
            TransactionUtil.commit(sqlSession );
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
